package com.shwread.android.ui.customview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import logic.bean.GoodsBaseInfo;
import logic.bean.GoodsPropInfo;
import logic.bean.GoodsSkuMapInfo;


/**
 * @author xuzhijun
 * @date 2015年4月11日上午9:05:11
 */
public class SkuUtil
{
	List<GoodsPropInfo> beforePropInfos ;
	
	public GoodsBaseInfo         goodsBaseInfo;
	/**
	 * 商品属性key集
	 */
	private List<List<String>>   keys         = new ArrayList<List<String>>();
	/**
	 * 商品属性key集排序之后
	 */
	public List<List<String>>    keys2        = new ArrayList<List<String>>();
	/**
	 * 商品属性value集
	 */
	private List<List<String>>   props        = new ArrayList<List<String>>();
	/**
	 * 商品属性value集排序之后
	 */
	public List<List<String>>    props2       = new ArrayList<List<String>>();
	/**
	 * 商品sku key集
	 */
	public List<List<String>>    skuKeysLList = new ArrayList<List<String>>();
	/**
	 * 商品sku value集
	 */
	public List<GoodsSkuMapInfo> skuValueList = new ArrayList<GoodsSkuMapInfo>();
	
	public SkuUtil(GoodsBaseInfo goodsBaseInfo) {
		keys = new ArrayList<List<String>>();
		this.goodsBaseInfo = goodsBaseInfo;
		initProp();
		initSku();
		cal();
	}
	
	/**
	 * 初始化属性prop
	 */
	private void initProp() {
		if (goodsBaseInfo.getProp() == null || goodsBaseInfo.getProp().size() == 0) { return; }
		for (int i = 0; i < goodsBaseInfo.getProp().size(); i++)
		{
			
			List<String> keyT = new ArrayList<String>();
			List<String> propT = new ArrayList<String>();
			for (int j = 0; j < goodsBaseInfo.getProp().get(i).getGoodsPropValueList().size(); j++)
			{
				if (goodsBaseInfo.getProp().get(i).isSku())
				{
					for (Entry<String, String> entry : goodsBaseInfo.getProp().get(i).getGoodsPropValueList().get(j).entrySet())
					{
						keyT.add(entry.getKey());
						propT.add(entry.getValue());
					}
				}
			}
			if (keyT.size() > 0)
			{
				keys.add(keyT);
				props.add(propT);
			}
		}
	}
	
	/**
	 * 初始化Sku
	 */
	private void initSku() {
		if (goodsBaseInfo.getSkuMap() == null || goodsBaseInfo.getSkuMap().size() == 0) { return; }
		for (Entry<String, GoodsSkuMapInfo> entry : goodsBaseInfo.getSkuMap().entrySet())
		{
			if (entry.getValue().getStock() > 0)
			{
				List<String> skus = new ArrayList<String>();
				String[] skuS = entry.getKey().split(";");
				for (String sku : skuS)
				{
					skus.add(sku);
				}
				skuKeysLList.add(skus);
				skuValueList.add(entry.getValue());
			}
		}
		tmpSkuLList = skuKeysLList;
	}
	
	private void cal() {
		beforePropInfos = goodsBaseInfo.getProp();
		List<GoodsPropInfo> newPropInfos = new ArrayList<GoodsPropInfo>();
		if (skuKeysLList != null && skuKeysLList.size() > 0)
		{
			for (int i = 0; i < skuKeysLList.get(0).size(); i++)
			{
				for (int j = 0; j < keys.size(); j++)
				{
					for (int j2 = 0; j2 < keys.get(j).size(); j2++)
					{
						if (skuKeysLList.get(0).get(i).equals(keys.get(j).get(j2)))
						{
							keys2.add(keys.get(j));
							props2.add(props.get(j));
							newPropInfos.add(beforePropInfos.get(j));
						}
					}
				}
			}
		}
		goodsBaseInfo.setProp(newPropInfos);
	}
	
	/**
	 * 从给定sku中 排除 不包含第position个属性propKey 返回筛选后的sku集
	 * 
	 * @param skuObj
	 * @param position
	 * @param propKey
	 * @return
	 */
	private List<List<String>> getSkuSingle(List<List<String>> skuObj, int position, String propKey) {
		List<List<String>> skuHave = new ArrayList<List<String>>();
		for (int i = 0; i < skuObj.size(); i++)
		{
			boolean isProp = false;
			List<String> skuList = new ArrayList<String>();
			for (int j = 0; j < skuObj.get(i).size(); j++)
			{
				if (position == j && skuObj.get(i).get(j).equals(propKey))
				{
					isProp = true;
				}
				skuList.add(skuObj.get(i).get(j));
			}
			if (isProp)
			{
				skuHave.add(skuList);
			}
		}
		return skuHave;
	}
	
	private List<List<String>> tmpSkuLList;
	
	/**
	 * 获取selector所选择的属性全部包含的sku
	 * 返回筛选的sku集
	 */
	public List<List<String>> getSku(Map<Integer, String> selector) {
		List<List<String>> skuLListTemp = skuKeysLList;
		for (Entry<Integer, String> entry : selector.entrySet())
		{
			skuLListTemp = getSkuSingle(skuLListTemp, entry.getKey(), entry.getValue());
		}
		StringBuilder returnSku = new StringBuilder();
		for (List<String> sku : skuLListTemp)
		{
			for (String skuList : sku)
			{
				if (returnSku.length() > 0)
				{
					returnSku.append(";");
				}
				returnSku.append(skuList);
			}
		}
		tmpSkuLList = skuLListTemp;
		return skuLListTemp;
	}
	
	/**
	 * @param sku
	 *            需要分析的sku
	 * @param selector
	 *            选择的sku属性{2,"3:20"} Map<属性位置(0开始), 属性key>
	 * @param x
	 *            需要判断的属性位置(0开始)
	 * @param y
	 *            需要判断的属性里的第几个位置(0开始)
	 * @return 判断x y是否可选
	 */
	public boolean getState(List<List<String>> sku, Map<Integer, String> selector, int x, int y) {
		String key = keys2.get(x).get(y);
		if (selector.containsKey(x))
		{
			Map<Integer, String> selectorT = new HashMap<Integer, String>();
			selectorT.putAll(selector);
			selectorT.remove(x);
			List<List<String>> skuT = getSku(selectorT);
			for (int i = 0; i < skuT.size(); i++)
			{
				if (skuT.get(i).get(x).equals(key)) { return true; }
			}
		}
		else
		{
			for (int i = 0; i < sku.size(); i++)
			{
				if (sku.get(i).get(x).equals(key)) { return true; }
			}
		}
		return false;
	}
	
	/**
	 * 返回所选sku的values
	 */
	public List<GoodsSkuMapInfo> getSkuValues() {
		List<List<String>> resultSku = tmpSkuLList;
		List<GoodsSkuMapInfo> skuInfo = new ArrayList<GoodsSkuMapInfo>();
		for (List<String> skuL : resultSku)
		{
			StringBuilder skuBuilder = new StringBuilder();
			for (String sku : skuL)
			{
				if (skuBuilder.length() > 0)
				{
					skuBuilder.append(";");
				}
				skuBuilder.append(sku);
			}
			
			for (GoodsSkuMapInfo goodsSkuMapInfo : skuValueList)
			{
				if (goodsSkuMapInfo.getProperties().equals(skuBuilder.toString()))
				{
					skuInfo.add(goodsSkuMapInfo);
				}
			}
		}
		return skuInfo;
	}
}
